import socket
import threading


class ClientSocket(threading.Thread):

    def __init__(self):
        # 没有该语句的话则会出现Python RuntimeError: thread.__init__() not called异常
        threading.Thread.__init__(self)
        self.udp_clien = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.udp_clien.bind(("", 8888))

    def send_msg(self):
        while True:
            msg = input("请输入数据")
            print(self.name)
            self.udp_clien.sendto(msg.encode("utf-8"), ("192.168.153.1", 8080))

    def rev_msg(self):
        while True:
            msg = self.udp_clien.recvfrom(1024)
            print(f"{self.name},{msg[0].decode('utf-8')},{msg[1]}")

    def run(self):
        t1 = threading.Thread(target=self.send_msg)
        t2 = threading.Thread(target=self.rev_msg)
        t1.start()
        t2.start()


if __name__ == "__main__":
    c = ClientSocket()
    c.start()
